home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / Quad.h < prev    next >
C/C++ Source or Header  |  1996-10-25  |  5KB  |  227 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_Quad_h)
  24. #define octave_Quad_h 1
  25.  
  26. #if defined (__GNUG__)
  27. #pragma interface
  28. #endif
  29.  
  30. #include <cfloat>
  31. #include <cmath>
  32.  
  33. #include "dColVector.h"
  34.  
  35. #if !defined (octave_Quad_typedefs)
  36. #define octave_Quad_typedefs 1
  37.  
  38. typedef double (*integrand_fcn) (double x);
  39.  
  40. #endif
  41.  
  42. // XXX FIXME XXX -- would be nice to not have to have this global
  43. // variable.
  44. // Nonzero means an error occurred in the calculation of the integrand
  45. // function, and the user wants us to quit.
  46. extern int quad_integration_error;
  47.  
  48. class
  49. Quad_options
  50. {
  51.  public:
  52.  
  53.   Quad_options (void) { init (); }
  54.  
  55.   // XXX FIXME XXX -- check for invalid values?
  56.   Quad_options (double abs, double rel)
  57.     : x_absolute_tolerance (abs), x_relative_tolerance (rel) { }
  58.  
  59.   Quad_options (const Quad_options& opt)
  60.     : x_absolute_tolerance (opt.x_absolute_tolerance),
  61.       x_relative_tolerance (opt.x_relative_tolerance) { }
  62.  
  63.   Quad_options& operator = (const Quad_options& opt)
  64.     {
  65.       if (this != &opt)
  66.     set_options (opt);
  67.  
  68.       return *this;
  69.     }
  70.  
  71.   ~Quad_options (void) { }
  72.  
  73.   void init (void)
  74.     {
  75.       double sqrt_eps = ::sqrt (DBL_EPSILON);
  76.  
  77.       x_absolute_tolerance = sqrt_eps;
  78.       x_relative_tolerance = sqrt_eps;
  79.     }
  80.  
  81.   void set_default_options (void) { init (); }
  82.  
  83.   void set_options (const Quad_options& opt)
  84.     {
  85.       x_absolute_tolerance = opt.x_absolute_tolerance;
  86.       x_relative_tolerance = opt.x_relative_tolerance;
  87.     }
  88.  
  89.   // XXX FIXME XXX -- check for invalid values?
  90.   void set_absolute_tolerance (double val) { x_absolute_tolerance = val; }
  91.   void set_relative_tolerance (double val) { x_relative_tolerance = val; }
  92.  
  93.   double absolute_tolerance (void) { return x_absolute_tolerance; }
  94.   double relative_tolerance (void) { return x_relative_tolerance; }
  95.  
  96.  private:
  97.  
  98.   double x_absolute_tolerance;
  99.   double x_relative_tolerance;
  100. };
  101.  
  102. class
  103. Quad : public Quad_options
  104. {
  105.  public:
  106.  
  107.   Quad (integrand_fcn fcn)
  108.     : Quad_options (), f (fcn) { }
  109.  
  110.   Quad (integrand_fcn fcn, double abs, double rel)
  111.     : Quad_options (abs, rel), f (fcn) { }
  112.  
  113.   virtual ~Quad (void) { }
  114.  
  115.   virtual double integrate (void)
  116.     {
  117.       int ier, neval;
  118.       double abserr;
  119.       return integrate (ier, neval, abserr);
  120.     }
  121.  
  122.   virtual double integrate (int& ier)
  123.     {
  124.       int neval;
  125.       double abserr;
  126.       return integrate (ier, neval, abserr);
  127.     }
  128.  
  129.   virtual double integrate (int& ier, int& neval)
  130.     {
  131.       double abserr;
  132.       return integrate (ier, neval, abserr);
  133.     }
  134.  
  135.   virtual double integrate (int& ier, int& neval, double& abserr) = 0;
  136.  
  137.  protected:
  138.  
  139.   integrand_fcn f;
  140. };
  141.  
  142. class
  143. DefQuad : public Quad
  144. {
  145.  public:
  146.  
  147.   DefQuad (integrand_fcn fcn)
  148.     : Quad (fcn), lower_limit (0.0), upper_limit (1.0), singularities () { }
  149.  
  150.   DefQuad (integrand_fcn fcn, double ll, double ul)
  151.     : Quad (fcn), lower_limit (ll), upper_limit (ul), singularities () { }
  152.  
  153.   DefQuad (integrand_fcn fcn, double ll, double ul, double abs,
  154.        double rel)
  155.     : Quad (fcn, abs, rel), lower_limit (ll), upper_limit (ul),
  156.       singularities () { }
  157.  
  158.   DefQuad (integrand_fcn fcn, double ll, double ul,
  159.        const ColumnVector& sing)
  160.     : Quad (fcn), lower_limit (ll), upper_limit (ul),
  161.       singularities (sing) { }
  162.  
  163.   DefQuad (integrand_fcn fcn, const ColumnVector& sing, double abs,
  164.        double rel)
  165.     : Quad (fcn, abs, rel), lower_limit (0.0), upper_limit (1.0),
  166.       singularities (sing) { }
  167.  
  168.   DefQuad (integrand_fcn fcn, const ColumnVector& sing)
  169.     : Quad (fcn), lower_limit (0.0), upper_limit (1.0),
  170.       singularities (sing) { }
  171.  
  172.   DefQuad (integrand_fcn fcn, double ll, double ul, const ColumnVector& sing,
  173.        double abs, double rel)
  174.     : Quad (fcn, abs, rel), lower_limit (ll), upper_limit (ul),
  175.       singularities (sing) { }
  176.  
  177.   ~DefQuad (void) { }
  178.  
  179.   double integrate (int& ier, int& neval, double& abserr);
  180.  
  181.  private:
  182.  
  183.   double lower_limit;
  184.   double upper_limit;
  185.  
  186.   ColumnVector singularities;
  187. };
  188.  
  189. class
  190. IndefQuad : public Quad
  191. {
  192.  public:
  193.  
  194.   enum IntegralType { bound_to_inf, neg_inf_to_bound, doubly_infinite };
  195.  
  196.   IndefQuad (integrand_fcn fcn)
  197.     : Quad (fcn), bound (0.0), type (bound_to_inf) { }
  198.  
  199.   IndefQuad (integrand_fcn fcn, double b, IntegralType t)
  200.     : Quad (fcn), bound (b), type (t) { }
  201.  
  202.   IndefQuad (integrand_fcn fcn, double b, IntegralType t, double abs,
  203.          double rel)
  204.     : Quad (fcn, abs, rel), bound (b), type (t) { }
  205.  
  206.   IndefQuad (integrand_fcn fcn, double abs, double rel)
  207.     : Quad (fcn, abs, rel), bound (0.0), type (bound_to_inf) { }
  208.  
  209.   ~IndefQuad (void) { }
  210.  
  211.   double integrate (int& ier, int& neval, double& abserr);
  212.  
  213.  private:
  214.  
  215.   double bound;
  216.   IntegralType type;
  217.   int integration_error;
  218. };
  219.  
  220. #endif
  221.  
  222. /*
  223. ;;; Local Variables: ***
  224. ;;; mode: C++ ***
  225. ;;; End: ***
  226. */
  227.